package eecs285.proj4.clientWin;

/** This class is the main frame of the 
 * client. It provide the list of the 
 * channels and a search in the list.
 * @author tywei
 */

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;

public class ClientMenuFrame extends JFrame
{
  private JTextField searchInput;
  private JButton searchButton;
  private JTree channelListTree;
  private JButton newChannelButton;
  private BackgroundPanel bgPanel;
  private ChannelPanel channel;
  private JButton quitButton;
  private NewChannelSettingDialog newChannelDialog;
  
  public ClientMenuFrame(String fileName)
  {
    super(fileName);
    
    setPreferredSize(new Dimension(1000, 700));
    pack();
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    //Search text field and button
    searchInput = new JTextField(15);
    searchInput.addKeyListener(new SearchKeyListener());  
    
    searchButton = new JButton("search");
    searchButton.addActionListener(new MenuFrameActionListener());

    
    JPanel searchPanel = new JPanel(new FlowLayout());
    searchPanel.add(searchInput);
    searchPanel.add(searchButton);
    
    //Tree of the channel list
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Broadcast Channels");
    createNodes(top);
    channelListTree = new JTree(top);
    channelListTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    ImageIcon leafIcon = new ImageIcon(getClass().getResource("/eecs285/proj4/clientWin/headphone.jpeg"));
    Image img = leafIcon.getImage();  
    Image newimg = img.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);  
    leafIcon = new ImageIcon(newimg);
    if (leafIcon != null) {
        DefaultTreeCellRenderer renderer = 
            new DefaultTreeCellRenderer();
        renderer.setLeafIcon(leafIcon);
        renderer.setOpenIcon(null);
        renderer.setClosedIcon(null);
        channelListTree.setCellRenderer(renderer);
    }
    JScrollPane treeView = new JScrollPane(channelListTree);
    treeView.setPreferredSize(new Dimension(20, 630));
    MouseListener ml = new MouseAdapter()
    {
      public void mousePressed(MouseEvent e)
      {
          DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                  channelListTree.getLastSelectedPathComponent();
          if(node!=null && node.isLeaf() && e.getClickCount() == 2)
          {
                  System.out.println("Select channel: " + node.toString());
                  channel = new ChannelPanel();
                  bgPanel.setVisible(false);
                  ClientMenuFrame.this.add(channel, BorderLayout.CENTER);
                  quitButton = new JButton("quit channel");
                  quitButton.addActionListener(new MenuFrameActionListener());
                  channel.rightPanel.add(quitButton, BorderLayout.SOUTH);
                  quitButton.setPreferredSize(new Dimension(100,50));
                  validate();
          }
      }
    };
    channelListTree.addMouseListener(ml);
    
    JPanel listPanel;
    listPanel = new JPanel(new BorderLayout());
    listPanel.add(searchPanel, BorderLayout.NORTH);
    listPanel.add(treeView, BorderLayout.SOUTH);
 
    setLayout(new BorderLayout());
    add(listPanel, BorderLayout.WEST);
    
  //Set the button for opening a new channel
    newChannelButton = new JButton("Open Your Own Channel");
    newChannelButton.setPreferredSize(new Dimension(300, 40));
    Font newTextFieldFont=new Font(newChannelButton.getFont().getName(),Font.BOLD, newChannelButton.getFont().getSize());    
    newChannelButton.setFont(newTextFieldFont); 
    newChannelButton.setForeground(Color.BLUE);
    newChannelButton.addActionListener(new NewChannelButtonListener());
    
    //Set the panel with the newChannelButton and background
    bgPanel = new BackgroundPanel();
    bgPanel.setLayout(new BorderLayout());
    JPanel buttonPanel = new JPanel(new FlowLayout());
    buttonPanel.add(newChannelButton, SwingConstants.CENTER);
    bgPanel.add(buttonPanel, BorderLayout.SOUTH);
    add(bgPanel, BorderLayout.CENTER);
    
    setVisible(true);
  }
  

  
  public void reset()
  {
    remove(channel);
    add(bgPanel, BorderLayout.CENTER);
  }
  
  private class NewChannelButtonListener implements ActionListener
  {
    public void actionPerformed(ActionEvent event)
    {
      if(event.getSource().equals(newChannelButton))
      {
        System.out.println("open a new channel");
        newChannelDialog = new NewChannelSettingDialog(ClientMenuFrame.this);
        newChannelDialog.setVisible(true);
        //if(newChannelDialog.confirm)
          
      }
    }
  }
  
  private class BackgroundPanel extends JPanel
  {
    // The Image to store the background image in.
    ImageIcon img;
    public BackgroundPanel()
    {
        // Loads the background image and stores in img object.
        img = new ImageIcon(getClass().getResource("/eecs285/proj4/tywei/headphone.jpeg"));
        Color transparent = new Color(0, true);
        this.setBackground(transparent);
    }

    public void paintComponent(Graphics g)
    {
        // Draws the img to the BackgroundPanel.
        g.drawImage(img.getImage(), 0, 0, getSize().width, getSize().height, this);
        super.paintComponent(g);
    }
  }
  
  private class MenuFrameActionListener implements ActionListener
  {
    public void actionPerformed(ActionEvent event)
    {
      if(event.getSource().equals(searchButton))
        searchList();
      else if(event.getSource().equals(quitButton))
      {
        System.out.println("quit channel");
        channel.setVisible(false);
        bgPanel.setVisible(true);
        ClientMenuFrame.this.validate();
      }
    }
  }
  
  private class SearchKeyListener extends KeyAdapter
  {
    public void keyReleased(KeyEvent event)
    {
      if(event.getKeyChar() == KeyEvent.VK_ENTER)
        searchList();
    }
  }
  
  private void searchList()
  {
    System.out.println("search: " + searchInput.getText());
  }
  
  private void createNodes(DefaultMutableTreeNode top)
  {
    DefaultMutableTreeNode category = null;
    DefaultMutableTreeNode book = null;
    
    category = new DefaultMutableTreeNode("Popular");
    top.add(category);
    
    book = new DefaultMutableTreeNode("The Java Tutorial: A Short Course on the Basics");
    category.add(book);
    
    book = new DefaultMutableTreeNode
        ("The Java Tutorial Continued: The Rest of the JDK");
    category.add(book);
    
    book = new DefaultMutableTreeNode("The Swing Tutorial: A Guide to Constructing GUIs");
    category.add(book);

    category = new DefaultMutableTreeNode("News");
    top.add(category);

    book = new DefaultMutableTreeNode("The Java Virtual Machine Specification");
    category.add(book);

    book = new DefaultMutableTreeNode("The Java Language Specification");
    category.add(book);
    
    category = new DefaultMutableTreeNode("Music");
    top.add(category);
 
    book = new DefaultMutableTreeNode("The Java Language Specification");
    category.add(book);
    
    category = new DefaultMutableTreeNode("Sports");
    top.add(category);
    
    book = new DefaultMutableTreeNode("The Java Language Specification");
    category.add(book);
    
    category = new DefaultMutableTreeNode("Others");
    top.add(category);
    
    book = new DefaultMutableTreeNode("The Java Language Specification");
    category.add(book);
  }
}